/*
 * DestinationsManager.cpp
 *
 *  Created on: Mar 16, 2009
 *      Author: Andrzej Olchawa
 *      mailto: andrzejolchawa@gmail.com
 *      http://www.hardtechnology.org
 *      This is a part of the Armazd project, which is covered by
 *      GNU GPL v2 license.
 */

#include "DestinationsManager.h"

DestinationsManager::DestinationsManager()
{
	listOfDestinations = new list<DestinationHost*>();

	if (listOfDestinations == NULL)
	{
		throw AgentException("DataSender(): can't allocate memory for listOfDestinations. \
							  The listOfDestinations is null");
	}
}

DestinationsManager::~DestinationsManager()
{
	clearDestinations();
	if (listOfDestinations)
	{
		delete listOfDestinations;
	}
}

list<DestinationHost*> *DestinationsManager::getDestinations()
{
	return listOfDestinations;
}

void DestinationsManager::addDestination(DestinationHost *destination)
{
	if (!listOfDestinations)
	{
		listOfDestinations = new list<DestinationHost*>();
	}
	if (listOfDestinations)
	{
		listOfDestinations->push_back(destination);
	}
}

void DestinationsManager::addDestination(string name, string host, int port)
{
	addDestination(new DestinationHost(name, host, port));
}

void DestinationsManager::removeDestination(DestinationHost *destination)
{
	if (listOfDestinations)
	{
		list<DestinationHost*>::iterator it = listOfDestinations->begin();
		do
		{
			if ((*it)->getName().compare(destination->getName()) == 0 &&
				(*it)->getHost().compare(destination->getHost()) == 0 &&
				(*it)->getPort() == destination->getPort())
			{
				delete *it;
				listOfDestinations->erase(it);
				it--;
			}
			it++;
		}
		while(it != listOfDestinations->end());
	}
}

void DestinationsManager::removeDestination(string name, string host, int port)
{
	DestinationHost *tmpDestinationHost = new DestinationHost(name, host, port);
	removeDestination(tmpDestinationHost);
	delete tmpDestinationHost;
}

bool DestinationsManager::containsDestination(DestinationHost *destination)
{
	if (listOfDestinations)
	{
		list<DestinationHost*>::iterator it = listOfDestinations->begin();
		do
		{
			if ((*it)->getName().compare(destination->getName()) == 0 &&
				(*it)->getHost().compare(destination->getHost()) == 0 &&
				(*it)->getPort() == destination->getPort())
			{
				return true;
			}
			it++;
		}
		while(it != listOfDestinations->end());
	}
	return false;
}

bool DestinationsManager::containsDestination(string name, string host, int port)
{
	DestinationHost *tmpDestinationHost = new DestinationHost(name, host, port);
	bool exists = containsDestination(tmpDestinationHost);
	delete tmpDestinationHost;

	return exists;
}

void DestinationsManager::printDestinations()
{
	if (listOfDestinations)
	{
		list<DestinationHost*>::iterator it;
		for (it=listOfDestinations->begin(); it!=listOfDestinations->end(); it++)
		{
			cout << (*it)->getName() << ": " <<(*it)->getHost() << " " << (*it)->getPort() << endl;
		}
	}
}

void DestinationsManager::clearDestinations()
{
	if (listOfDestinations)
	{
		list<DestinationHost*>::iterator it;
		for (it=listOfDestinations->begin(); it!=listOfDestinations->end(); it++)
		{
			delete *it;
		}
	}
}
